package com.resttemplate.controller;

import com.resttemplate.entity.StudentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@RestController
@RequestMapping("/rest")
public class RestHandler {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/all")
    public Collection<StudentEntity> all(){
        return restTemplate.getForEntity("http://localhost:8010/student/all",Collection.class).getBody();
    }
    @GetMapping("/all2")
    public Collection<StudentEntity> all2(){
        return restTemplate.getForObject("http://localhost:8010/student/all",Collection.class);
    }
    @GetMapping("/buId/{id}")
    public StudentEntity byId(@PathVariable("id")Long id){
        return restTemplate.getForEntity("http://localhost:8010/student/Byid/{id}",StudentEntity.class,id).getBody();
    }
    @GetMapping("/buId2/{id}")
    public StudentEntity byId2(@PathVariable("id")Long id){
        return restTemplate.getForObject("http://localhost:8010/student/Byid/{id}",StudentEntity.class,id);
    }
    @PostMapping("/save")
    public void save(@RequestBody StudentEntity studentEntity){
        restTemplate.postForEntity("http://localhost:8010/student/save",studentEntity,null).getBody();
    }
    @PostMapping("/save2")
    public void save2(@RequestBody StudentEntity studentEntity){
        restTemplate.postForObject("http://localhost:8010/student/save",studentEntity,String.class);
    }
    @PutMapping("/update")
    public void update(@RequestBody StudentEntity studentEntity){
        restTemplate.put("http://localhost:8010/student/save",studentEntity);
    }
    @DeleteMapping("/del/{id}")
    public void del(@PathVariable("id")Long id){
        restTemplate.delete("http://localhost:8010/student/del/{id}",id);
    }
}
